package com.zoe.wan.android.example.activity.webView

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ActivityWebViewBinding
import com.zoe.wan.base.BaseActivity
import com.zoe.wan.android.example.BR

class WebActivity : BaseActivity<ActivityWebViewBinding, WebViewModel>() {
    companion object {
        const val INTENT_WEB_TITLE_KEY = "INTENT_WEB_TITLE_KEY"
        const val INTENT_WEB_URL_KEY = "INTENT_WEB_URL_KEY"
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_web_view
    }

    override fun getViewModelId(): Int {
        return BR.webVm
    }

    override fun initViewData() {
        val title = intent.getStringExtra(INTENT_WEB_TITLE_KEY)
        val url = intent.getStringExtra(INTENT_WEB_URL_KEY)

        viewModel?.webTitle?.set(title)

        binding?.webViewBack?.setOnClickListener {
            finish()
        }

        binding?.webView?.loadUrl(url ?: "")
        initWebViewSettings()
    }


    private fun initWebViewSettings() {
        binding?.webView?.settings?.apply {
            // 支持js脚本
            this.javaScriptEnabled = true
            // 启用DOM存储
            this.domStorageEnabled = true
            // 启用混合内容模式
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                this.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
            }

            // 屏幕自适应
            this.useWideViewPort = true
            this.loadWithOverviewMode = true
            // 缩放操作
            this.setSupportZoom(true)
            // 设置内置的缩放控件，若为false则不可缩放
            this.builtInZoomControls = true
            // 隐藏原生缩放控件
            this.displayZoomControls = false

            // 其他
            // 关闭webview中的缓存
            this.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
            //允许访问文件
            this.allowFileAccess = true
            //支持通过js打开新的窗口
            this.javaScriptCanOpenWindowsAutomatically = true
            // 支持自动加载图片
            this.loadsImagesAutomatically = true
            // 设置编码格式
            this.defaultTextEncodingName = "utf-8"

        }

        // 设置只能内部浏览器打开url
        binding?.webView?.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                url?.let {
                    if (it.startsWith("http") || it.startsWith("https")) {
                        view?.loadUrl(it)
                        return true
                    } else {
                        // 使用Intent处理其他自定义协议
                        try {
                            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(it))
                            view?.context?.startActivity(intent)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                        return true
                    }
                }
                return false
            }
        }
    }
}